//
//  UIViewController+Extension.m
//  Community
//
//  Created by Pengxu on 25/10/2016.
//  Copyright © 2016 MaryKayChina. All rights reserved.
//

#import "UIViewController+Extension.h"

@implementation UIViewController (Extension)

- (void)addBackItem {

    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"]
                                                                 style:UIBarButtonItemStylePlain
                                                                target:self
                                                                action:@selector(clickBackItem)];
    self.navigationItem.leftBarButtonItem = backItem;

    
}

- (void)addBackItemWhiteIcon{
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back_binli"]
                                                                 style:UIBarButtonItemStylePlain
                                                                target:self
                                                                action:@selector(clickBackItem)];
    self.navigationItem.leftBarButtonItem = backItem;
}

- (void)addBackItem2 {

    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"]
                                                                 style:UIBarButtonItemStylePlain
                                                                target:self
                                                                action:@selector(clickBackItem2)];
    self.navigationItem.leftBarButtonItem = backItem;
    

}

- (void)addRightItem:(NSString *)title {

    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:self action:@selector(rightTopAction)];
    self.navigationItem.rightBarButtonItem = rightItem;
    
}

- (void)setNavigationItemHidden:(BOOL)navBarHidden statusBarHidden:(BOOL)statusbarHidden
{
    [UIApplication sharedApplication].statusBarHidden = statusbarHidden;
    self.navigationController.navigationBar.hidden = navBarHidden;
}

- (void)setNavigationRightItem:(NSString *)numerator denominator:(NSString *)denominator
{
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] init];
    
    
    NSAttributedString *attr1 = [[NSAttributedString alloc] initWithString: [NSString stringWithFormat:@"%@/",numerator]
                                                                attributes:@{
                                                                             NSForegroundColorAttributeName: [UIColor whiteColor],
                                                                             NSFontAttributeName : FONT_PINGFANG_REGULAR_(18)
                                                                             }];
    NSAttributedString *attr2 = [[NSAttributedString alloc] initWithString:denominator
                                                                attributes:@{
                                                                             NSForegroundColorAttributeName: ColorFromRGB(0xb5adea),
                                                                             NSFontAttributeName : FONT_PINGFANG_REGULAR_(12)
                                                                             }];
    [attrStr appendAttributedString:attr1];
    [attrStr appendAttributedString:attr2];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 44)];
    titleLabel.textAlignment = NSTextAlignmentRight;
    titleLabel.attributedText = attrStr;
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:titleLabel];
    self.navigationItem.rightBarButtonItem = rightItem;
}



- (void)clickBackItem{
 [self.navigationController popViewControllerAnimated:YES];
}

- (void)clickBackItem2{

    [self.navigationController dismissViewControllerAnimated:YES
                                                  completion:^{

                                                  }];

}

- (void)rightTopAction{

}

- (void)popToViewControllers:(NSArray*)aimViewControllers
{
    if (!self.navigationController) {
        return;
    }
    if (aimViewControllers.count == 0) {
        return;
    }
    if (self.navigationController.viewControllers.count <= 1) {
        return;
    }
    UIViewController* popToVC = nil;
    for (int i = (int)(self.navigationController.viewControllers.count-1); i >= 0; i--) {
        UIViewController* vc = [self.navigationController.viewControllers objectAtIndex:i];
        BOOL flag = NO;
        for (int j=0; j < aimViewControllers.count; j++) {
            Class cls = NSClassFromString(SAFE_STRING([aimViewControllers objectAtIndex:j]));
            if (cls == NULL) {
                continue;
            }
            if ([vc isKindOfClass:cls]) {
                popToVC = vc;
                flag = YES;
                break;
            }
        }
        if (YES == flag) {
            break;
        }
    }
    
    if (popToVC) {
        [self.navigationController popToViewController:popToVC animated:YES];
    }else{
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
}

@end
